//
//  UserPublicInfoVO.m
//  GolfFriend
//
//  Created by Vescky on 13-11-26.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "UserPublicInfoVO.h"
#import <objc/runtime.h>

@implementation UserPublicInfoVO
@synthesize recordId,userId,sex,avatar,motto,area,teams,lastLoginCity,prestige,positionFactor,nickName;
@synthesize lastLoginLatitude,lastLoginLongitude,lastLoginPosition,lastLoginProvince,lastLoginTime,lastLoginToken;
@synthesize enName,teamLogo,teamName;

- (id)init{
    self = [super init];
    
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    id copy = [[[self class] alloc] init];
    if (copy) {
        [copy setRecordId:self.recordId];
        [copy setUserId:self.userId];
        [copy setSex:self.sex];
        [copy setAvatar:self.avatar];
        [copy setMotto:self.motto];
        [copy setArea:self.area];
        [copy setTeams:self.teams];
        [copy setLastLoginTime:self.lastLoginTime];
        [copy setLastLoginPosition:self.lastLoginProvince];
        [copy setLastLoginCity:self.lastLoginCity];
        [copy setLastLoginPosition:self.lastLoginPosition];
        [copy setLastLoginToken:self.lastLoginToken];
        [copy setPrestige:self.prestige];
        [copy setNickName:self.nickName];
        [copy setEnName:self.enName];
        [copy setTeamLogo:self.teamLogo];
        [copy setTeamName:self.teamName];
        [copy setPositionFactor:self.positionFactor];
        [copy setLastLoginLatitude:self.lastLoginLatitude];
        [copy setLastLoginLongitude:self.lastLoginLongitude];
    }
    return copy;
}

- (NSDictionary *)toDictionary
{
    NSMutableDictionary *dictionaryFormat = [NSMutableDictionary dictionary];
    //  取得当前类类型
    Class cls = [self class];
    
    unsigned int ivarsCnt = 0;
    //　获取类成员变量列表，ivarsCnt为类成员数量
    Ivar *ivars = class_copyIvarList(cls, &ivarsCnt);
    
    //　遍历成员变量列表，其中每个变量都是Ivar类型的结构体
    for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p)
    {
        Ivar const ivar = *p;
        //　获取变量名
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        // 若此变量未在类结构体中声明而只声明为Property，则变量名加前缀 '_'下划线
        // 比如 @property(retain) NSString *abc;则 key == _abc;
        //　获取变量值
        id value = [self valueForKey:key];
        //　取得变量类型
        // 通过 type[0]可以判断其具体的内置类型
        //        const char *type = ivar_getTypeEncoding(ivar);
        if (value)
        {
            [dictionaryFormat setObject:value forKey:key];
        }
    }
    
    return dictionaryFormat;
    
}

- (void)dictionaryToUserPublicInfoVO:(NSDictionary*)_dict {
    if (![_dict isKindOfClass:[NSDictionary class]] || !_dict) {
        return;
    }
    [self setRecordId:[_dict objectForKey:@"recordId"]];
    [self setUserId:[_dict objectForKey:@"userId"]];
    [self setSex:[_dict objectForKey:@"sex"]];
    [self setAvatar:[_dict objectForKey:@"avatar"]];
    [self setMotto:[_dict objectForKey:@"motto"]];
    [self setArea:[_dict objectForKey:@"area"]];
    [self setTeams:[_dict objectForKey:@"teams"]];
    [self setLastLoginTime:[_dict objectForKey:@"lastLoginTime"]];
    [self setLastLoginProvince:[_dict objectForKey:@"lastLoginProvince"]];
    [self setLastLoginCity:[_dict objectForKey:@"lastLoginCity"]];
    [self setLastLoginPosition:[_dict objectForKey:@"lastLoginPosition"]];
    [self setLastLoginToken:[_dict objectForKey:@"lastLoginToken"]];
    [self setPrestige:[[_dict objectForKey:@"prestige"] intValue]];
    [self setPositionFactor:[[_dict objectForKey:@"positionFactor"] floatValue]];
    [self setLastLoginLatitude:[[_dict objectForKey:@"lastLoginLatitude"] floatValue]];
    [self setLastLoginLongitude:[[_dict objectForKey:@"lastLoginLongitude"] floatValue]];
    [self setNickName:[_dict objectForKey:@"nickName"]];
    [self setEnName:[_dict objectForKey:@"enName"]];
    [self setTeamName:[_dict objectForKey:@"teamName"]];
    [self setTeamLogo:[_dict objectForKey:@"teamLogo"]];
}

@end
